$(function () {
    initDg();
});

function initDg() {
    $('#dg').datagrid({
        width: '100%',
        height: 'auto',
        url: '/webSiteSetting/list',
        fitColumns: true,
        autoRowHeight: false,
        striped: true,
        method: "GET",
        idField: "fdId",
        loadMsg: "正在加载数据...",
        pagination: true,
        rownumbers: true,
        pagePosition: 'bottom',
        pageSize: 50,
        pageList: [20, 30, 40, 50],
        showFooter: true,
        fit: true,
        queryParams: {
            name: $('#username').val()
        },
        columns: [[
            {field: 'fdId', title: 'ID', width: 100, align: 'center', checkbox: true},
            {
                field: 'fdType',
                title: '公告类型',
                width: 100,
                formatter:function(value,row,index){
                    if(value === 0){
                        return "系统公告";
                    }else if(value === 1){
                        return "活动公告";
                    }else if(value === 2){
                        return "新闻资讯";
                    }
                }
            },
            {
                field: 'fdGbPic',
                title: '顶部背景图',
                width: 100
            },
            {
                field: 'fdTitle',
                title: '公告标题',
                width: 100
            },
            {
                field: 'fdContent',
                title: '公告内容',
                width: 100,
                formatter:function(value,row,index){
                    var temp = JSON.stringify(row)
                    return "<a href='javascript:void(0)' onclick='showContent("+temp+")'>查看内容</a>";
                }
            },
            {
                field: 'fdColor',
                title: '标题颜色',
                width: 100,
                styler: function(value,row,index){
                    if (value){
                        return 'background-color:'+value+";";
                    }else{
                        return 'background-color;#FFFFFF';
                    }
                }

            },
            {
                field: 'fdIsEnable',
                title: '是否启用',
                width: 100,
                formatter:function(value,row,index){
                    if(value === 1){
                        return "启用";
                    }else if(value === 0){
                        return "禁用";
                    }
                }
            },
            {
                field: 'fdCreatedTime',
                title: '创建时间',
                width: 100
            },
            {
                field: 'fdOrder',
                title: '排序',
                width: 100
            }
        ]],
        toolbar: '#tb'
    });
}

function edit() {
    var row = $('#dg').datagrid('getSelections');
    if (row != null && row.length == 1) {
        var id = row[0].fdId;
        $('#win').window({
            width: 900,
            height: 700,
            modal: true,
            border:false,
            collapsible: false,
            minimizable: false,
            maximizable: false,
            resizable: true,
            iconCls: "icon-add",
            content: "<iframe src='/webSiteSetting/edit/" +id+" scrolling='auto' width='100%' height='100%' frameborder='0px'></iframe>",
            title: "编辑"

        });
    } else {
        $.messager.alert('提示', '请选中一行数据操作!', 'Warning');
    }

}

function add() {
    $('#win').window({
        width: 900,
        height: 700,
        modal: true,
        border:false,
        collapsible: false,
        minimizable: false,
        maximizable: false,
        resizable: true,
        iconCls: "icon-add",
        content: "<iframe src='../../../admin/webSiteSetting/add.jsp' scrolling='auto' width='100%' height='100%' frameborder='0px'></iframe>",
        title: "添加"
    });
}

function remove() {
    var row = $('#dg').datagrid('getSelections');
    if (row.length === 0) {
        $.messager.alert('提示', '请选中一行数据操作!', 'Warning');
    } else {
        var ids = new Array();
        $.each(row, function (i, row) {
            ids[i] = row['fdId'];
        });
        $.messager.confirm('提示', '确认删除该条记录?', function (r) {
            if (r) {
                console.log(ids);
                $.ajax({
                    type: 'POST',
                    url: '/webSiteSetting/remove',
                    data: {"ids": ids},
                    success: function (r) {
                        if (r.code == 0) {
                            $.messager.alert('提示', r.msg, 'info');
                            reLoad();
                            clearSelections();
                        } else {
                            $.messager.alert('提示', r.msg, 'Warning');
                        }
                    }
                });
            }
        });
    }
}

function showContent(c) {
    $('#showWin').window({
        width: 500,
        height: 600,
        modal: true,
        border:true,
        collapsible: false,
        minimizable: false,
        maximizable: false,
        resizable: true,
        iconCls: "icon-tip",
        content: "<div align='center'>"+c.fdContent+"</div>",
        title: c.fdTitle,
        onClose:function () {
            clearSelections();
        }
    });
}

function reLoad() {
    initDg();
}

function closeWin() {
    $("#win").window("close");
}

function clearSelections() {
    $("#dg").datagrid("clearSelections");
}

